#include <stdio.h>
#include <stdlib.h>

int n=0,option;
int *list;


void main()
{
int i=0;
printf("Enter the size of list: ");
scanf("%d",&n);
list=(int *)malloc(n*sizeof(int));
for (i=0;i<n;i++)
	{	
	printf("Enter element no. %d of the list: ",i+1);
	scanf("%d", list+i);
	}
menu();
}


void menu()
{
int app;
printf("\nWhat function do you want to apply on your list?\n\n1.Display the list\n2.Reverse the list\n3.Sort the list\n4.Pop an element\n5.Extend the list\n6.Append a list\n7.Find index of an element\n8.Delete an element\n9.Find count of an element in the list\n10.Insert an element at a given position\n11. Exit the program\n\nChoose the no. of an option ");
scanf("%d",&option);
switch(option)
    { 
      case 1:display();break;
      case 2:reverse();break;
      case 3:sort();break;
      case 4:pop();break;
      case 5:extend();break;
      case 6:printf("\nEnter element to be appended: ");scanf("%d", &(app));append(app);display();menu();break;
      case 7:index_no();break;
      case 8:delete();break;
      case 9:count();break;
      case 10:insert();break;
      case 11:exit(0);break;
      default:printf("Wrong choice, enter your choice again.");menu();break;
    }
}

void sort()
{
int i,j,temp;
for (i=0;i<n-1;i++)
	{
	for (j=i+1;j<n;j++)
		{
		if (list[i]>list[j])
			{
			temp=list[i];
			list[i]=list[j];
			list[j]=temp;
			}
		}
	}
printf("Sorted list is printed below\n");
display();
}

void append(int j)
{
int i;
list=realloc(list, (n+1)*sizeof(int));
list[n]=j;
n++;
}

void extend()
{
int n2,i;
int *list2;
printf("Enter the size of the list to be extended in the current list: ");
scanf("%d",&n2);
list2=(int *)malloc(n2*sizeof(int));
for (i=0;i<n2;i++)
	{	
	printf("Enter element no. %d of the new list: ",i+1);
	scanf("%d", list2+i);
	}
for (i=0;i<n2;i++)
	{
	append(list2[i]);
	}
display();
menu();
}

void index_no()
{
int num,i,flag=0,count=0;
printf("\nEnter the number of which you want the index: ");
scanf("%d",&num);
for (i=0;i<n;i++)
	{
	if (list[i]==num)
		{
		flag=1;
		count++;
		printf("\nThe index of element is %d\n",count);
		}
	else count++;
	}
if (flag==0)
	printf("\nElement not found.\n");
menu();
}

void pop()
{
int i,ind;
printf("\nEnter index of element to be popped: ");
scanf("%d", &ind);
printf("\nElement that will be popped is: %d\n", list[ind]);
for(i=ind-1;i<n;i++)
	{
	list[i]=list[i+1];
	}
list= (int *)realloc(list,(sizeof(int)*(n-1)));
n--;
display();
}

void insert()
{
int pos,i,num,temp;
printf("\nEnter position at which number is to be inserted: ");
scanf("%d", &pos);
printf("\nEnter number to be inserted: ");
scanf("%d", &num);
list=(int *)realloc(list, (n+1)*sizeof(int));
temp=list[pos-1];
list[pos-1]=num;
for(i=pos;i<n;i++)
	{
        list[i+1]=list[i];
	}
list[pos]=temp;
n++;
display();
menu;
}


void reverse()
{

}

void count()
{

}

void delete()
{

}

void display()
{
int i;
printf("\nTotal number of elements in list are %d.\nThe list is: [", n);
for (i=0;i<n-1;i++)
	{
	printf("%d, ", list[i]);
	}
printf("%d]\n",list[i]);
}
